﻿[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.Additions.Determinacy.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.Additions.Determinacy.debug.js", "application/x-javascript")]

namespace BM.Tools.WebControls.MForm.Controls.Additions
{
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Linq;
    using System.Web.UI;

    using AjaxControlToolkit;

    using BM.Tools.WebControls.MForm.Controls.Helper;
    using BM.Tools.WebControls.MForm.Controls.Items;
    using BM.Tools.WebControls.MForm.ScriptReferences;

    /// <summary>
    /// The constraint that performs some action on one item if another item meets certain condition
    /// </summary>
    [ParseChildren(true, "Dependants")]
    [ClientScriptResource("BM.Tools.WebControls.MForm", "BM.Tools.WebControls.MForm.Controls.Additions.Determinacy.js")]        
    public class Determinacy : Constraint
    {        
        private Collection<Dependant> dependants = new Collection<Dependant>();        

        /// <summary>
        /// Initializes a new instance of the <see cref="Determinacy"/> class.
        /// </summary>
        public Determinacy()
        {
            Operator = DeterminacyOperator.Equals;
        }

        /// <summary>
        /// Gets Dependants.
        /// </summary>   
        [AttributeProperty(Name = "dependants")]     
        [TypeConverter(typeof(JsonConverter))]
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public Collection<Dependant> Dependants
        {
            get
            {
                return dependants;
            }
        }

        /// <summary>
        /// Gets or sets the method name
        /// this attribute can be used to provide a method that will be called on the javascript item object
        /// by default the "getValue" method is called, but if the index of the item should be compared,
        /// the Method property can be set to "getIndex", if the name is to be compared, the "getName" can be used,        
        /// </summary>
        [AttributeProperty(Name = "method")]
        public string Method
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets value to compare.
        /// </summary>        
        [AttributeProperty(Name = "value")]
        public string ValueToCompare
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets Value.
        /// </summary>        
        [AttributeProperty(Name = "operator")]
        [DefaultValue(DeterminacyOperator.Equals)]
        public DeterminacyOperator Operator
        {
            get;
            set;
        }

        /// <summary>
        /// This method is always true
        /// </summary>
        /// <param name="item">
        /// The item on which the validation is done.
        /// </param>
        /// <returns>
        /// always true
        /// </returns>
        public override bool IsValid(OffspringItem item)
        {
            return true;
        }

        /// <summary>
        /// does the standard OnPreRender and includes resources
        /// </summary>
        /// <param name="e">
        /// The event args.
        /// </param>
        protected override void OnPreRender(System.EventArgs e)
        {
            base.OnPreRender(e);
            ScriptIncluder.IncludeResources(Page, typeof(Dependant));
            foreach (var type in Dependants.SelectMany(dependant => dependant.Actions).Select(action => action.GetType()).Distinct())
            {
                ScriptIncluder.IncludeResources(Page, type);
            }
        }
    }
}